//
//  SetPasswordVC.m
//  HengYouCai
//
//  Created by ycgwl on 2017/5/1.
//  Copyright © 2017年 lwj. All rights reserved.
//

#import "SetPasswordVC.h"

@interface SetPasswordVC ()
@property (weak, nonatomic) IBOutlet UIButton *OkBtn;
@property (nonatomic, retain)UITextField *pswTF;
@end

@implementation SetPasswordVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"设置交易密码";

}
- (IBAction)eyeAnduneye:(UIButton *)sender {
  
    sender.selected = !sender.selected;
    
    [_pswTF setSecureTextEntry:!_pswTF.secureTextEntry];
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [_pswTF.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
        if (x.length==6) {
            _OkBtn.enabled = YES;
            [_OkBtn setBackgroundColor:Normal_RedColor];
            
        }else{
            if (x.length>6) {
                [self.view showHUDWarnTips:@"请输入6位交易密码" hideDelay:1.5];
            }
            _OkBtn.enabled = NO;
            [_OkBtn setBackgroundColor:Normal_UnselecBtnColor];
            
        }
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PasswordCellID"];
    UITextField *field = [cell viewWithTag:1002];
    self.pswTF = field;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    UITextField *field = [cell viewWithTag:1002];
    [field becomeFirstResponder];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.y<-130) {
        [self.view endEditing:YES];
        
    }
}
- (IBAction)setting:(id)sender {
    
    WLGetWeakSelf(weakSelf, self);
    
    if (_isReset) {
        [HYCNetService accountResetPayPasswordWithDict:@{@"newPassword":weakSelf.pswTF.text} onSuccess:^(NSDictionary *dictResult) {
            //
            [WLFileManager saveUserData:weakSelf.pswTF.text forKey:kUserPaypassword];
            [weakSelf.tableView showHUDSuccessTips:@"修改交易密码成功！" hideDelay:1];
            [weakSelf performSelector:@selector(popTheVC) withObject:nil afterDelay:1];
        } andFailed:^(NSInteger code, NSString *errorMsg) {
            //
            [weakSelf.view showHUDFailTips:errorMsg hideDelay:1];
        }];
    }else{
        [HYCNetService accountSetTradePasswordWithDict:@{@"tradePassword":weakSelf.pswTF.text} onSuccess:^(NSDictionary *dictResult) {
            //
            [weakSelf.tableView showHUDSuccessTips:@"设置交易密码成功！" hideDelay:1];
            [weakSelf performSelector:@selector(popTheVC) withObject:nil afterDelay:1];
            [WLFileManager saveUserData:weakSelf.pswTF.text forKey:kUserPaypassword];
        } andFailed:^(NSInteger code, NSString *errorMsg) {
            //
            [weakSelf.view showHUDFailTips:errorMsg hideDelay:1];
        }];
    }
   
    
    
}
- (void)popTheVC{
    [self popToViewControllerAtIndex:1];

}
@end
